import 'package:flutter/material.dart';
import 'order_page.dart';
import 'chat_page.dart';
import '../main.dart';
import '../widgets/loading.dart';  // 添加导入

class OrderDetailPage extends StatelessWidget {
  final OrderItem order;

  const OrderDetailPage({
    super.key,
    required this.order,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xFFF7F7F7),
      appBar: AppBar(
        title: const Text(
          '订单详情',
          style: TextStyle(
            fontWeight: FontWeight.w600,
            color: Color(0xFF333333),
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back_ios,
            color: Color(0xFF333333),
            size: 18,
          ),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      body: Stack(
        children: [
          // 主内容区域
          ListView(
            padding: const EdgeInsets.fromLTRB(15, 15, 15, 80), // 底部留出按钮的空间
            children: [
              // 订单状态卡片
              _buildStatusCard(context),
              const SizedBox(height: 12),
              // 地址信息卡片
              _buildAddressCard(),
              const SizedBox(height: 12),
              // 服务信息卡片
              _buildServiceCard(),
              const SizedBox(height: 12),
              // 订单信息卡片
              _buildOrderInfoCard(),
            ],
          ),
          // 底部固定的操作按钮
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            child: _buildBottomButtons(context),
          ),
        ],
      ),
    );
  }

  Widget _buildStatusCard(BuildContext context) {
    return InkWell(
      onTap: () => _showServiceProgress(context),
      child: Container(
        padding: const EdgeInsets.all(15),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 10,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Row(
          children: [
            Container(
              padding: const EdgeInsets.all(10),
              decoration: BoxDecoration(
                color: order.getStatusColor().withOpacity(0.1),
                shape: BoxShape.circle,
              ),
              child: Icon(
                order.getStatusIcon(),
                color: order.getStatusColor(),
                size: 24,
              ),
            ),
            const SizedBox(width: 12),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  order.getStatusText(),
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.w600,
                    color: order.getStatusColor(),
                  ),
                ),
                const SizedBox(height: 4),
                Text(
                  _getStatusDescription(),
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey[600],
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  void _showServiceProgress(BuildContext context) {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) => Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.vertical(
            top: Radius.circular(16),
          ),
        ),
        child: SafeArea(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                height: 52,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color(0xFFEEEEEE),
                      width: 0.5,
                    ),
                  ),
                ),
                child: const Text(
                  '服务进度',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.fromLTRB(20, 15, 20, 25),
                child: Column(
                  children: [
                    _buildProgressItem(
                      context,
                      icon: Icons.check_circle,
                      title: '订单已提交',
                      time: '2024-02-05 14:00',
                      isCompleted: true,
                      isFirst: true,
                    ),
                    _buildProgressItem(
                      context,
                      icon: Icons.check_circle,
                      title: '师傅已接单',
                      time: '2024-02-05 14:05',
                      description: '张师傅（138****8888）已接单',
                      isCompleted: true,
                    ),
                    _buildProgressItem(
                      context,
                      icon: Icons.local_shipping,
                      title: '正在服务',
                      time: '2024-02-05 14:30',
                      description: '师傅正在为您搬运物品',
                      isCompleted: order.status == 'completed',
                      isActive: order.status == 'serving',
                    ),
                    _buildProgressItem(
                      context,
                      icon: Icons.check_circle,
                      title: '服务完成',
                      time: order.status == 'completed' ? '2024-02-05 15:30' : '',
                      isCompleted: order.status == 'completed',
                      isLast: true,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildProgressItem(
    BuildContext context, {
    required IconData icon,
    required String title,
    required String time,
    String? description,
    bool isCompleted = false,
    bool isActive = false,
    bool isFirst = false,
    bool isLast = false,
  }) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        // 左侧时间
        SizedBox(
          width: 85,
          child: Text(
            time,
            style: TextStyle(
              fontSize: 12,
              color: isCompleted || isActive ? Colors.grey[600] : Colors.grey[400],
            ),
          ),
        ),
        // 中间进度线和图标
        Column(
          children: [
            Icon(
              icon,
              size: 20,
              color: isCompleted || isActive
                  ? Theme.of(context).colorScheme.primary
                  : Colors.grey[300],
            ),
            if (!isLast)
              Container(
                width: 1,
                height: description != null ? 50 : 30,
                color: isCompleted
                    ? Theme.of(context).colorScheme.primary
                    : Colors.grey[300],
              ),
          ],
        ),
        const SizedBox(width: 12),
        // 右侧内容
        Expanded(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Text(
                title,
                style: TextStyle(
                  fontSize: 14,
                  fontWeight: FontWeight.w500,
                  color: isCompleted || isActive
                      ? const Color(0xFF333333)
                      : Colors.grey[400],
                ),
              ),
              if (description != null) ...[
                const SizedBox(height: 4),
                Text(
                  description,
                  style: TextStyle(
                    fontSize: 13,
                    color: isCompleted || isActive
                        ? Colors.grey[600]
                        : Colors.grey[400],
                  ),
                ),
              ],
              if (!isLast)
                const SizedBox(height: 16),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildAddressCard() {
    return Builder(
      builder: (context) => Container(
        padding: const EdgeInsets.all(15),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 10,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    color: Colors.blue.withOpacity(0.1),
                    shape: BoxShape.circle,
                  ),
                  child: const Icon(
                    Icons.location_on,
                    color: Colors.blue,
                    size: 20,
                  ),
                ),
                const SizedBox(width: 10),
                const Text(
                  '地址信息',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const Spacer(),
                // 联系客服按钮
                IconButton(
                  onPressed: () {
                    // 跳转到聊天页面
                    Navigator.push(
                      context,
                      MaterialPageRoute(
                        builder: (context) => const ChatPage(title: '在线客服'),
                      ),
                    );
                  },
                  icon: Icon(
                    Icons.support_agent,
                    color: Theme.of(context).colorScheme.primary,
                    size: 20,
                  ),
                  style: IconButton.styleFrom(
                    padding: const EdgeInsets.all(8),
                  ),
                ),
                // 修改订单按钮
                if (order.status == 'pending_accept')
                  TextButton.icon(
                    onPressed: () => _handleEditOrder(context),
                    icon: const Icon(
                      Icons.edit_outlined,
                      size: 16,
                    ),
                    label: const Text('修改订单'),
                    style: TextButton.styleFrom(
                      foregroundColor: Theme.of(context).colorScheme.primary,
                      padding: const EdgeInsets.symmetric(
                        horizontal: 12,
                        vertical: 6,
                      ),
                      textStyle: const TextStyle(
                        fontSize: 13,
                        fontWeight: FontWeight.w500,
                      ),
                    ),
                  ),
              ],
            ),
            const SizedBox(height: 15),
            // 搬出地址
            _buildAddressItem(
              icon: Icons.upload_outlined,
              address: '北京市朝阳区三里屯街道',
              detail: '2号楼3单元',
              floor: '8楼',
              hasElevator: true,
              contact: '张先生 13800138000',
              showDivider: true,
              showContact: true,
            ),
            // 搬入地址
            _buildAddressItem(
              icon: Icons.download_outlined,
              address: '北京市海淀区中关村街道',
              detail: '6号楼1单元',
              floor: '6楼',
              hasElevator: false,
              contact: '张先生 13800138000',
              showContact: false,
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildAddressItem({
    required IconData icon,
    required String address,
    required String detail,
    required String floor,
    required bool hasElevator,
    required String contact,
    bool showDivider = false,
    bool showContact = true,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Container(
              padding: const EdgeInsets.all(6),
              decoration: BoxDecoration(
                color: Colors.blue.withOpacity(0.1),
                borderRadius: BorderRadius.circular(6),
              ),
              child: Icon(
                icon,
                color: Colors.blue,
                size: 16,
              ),
            ),
            const SizedBox(width: 8),
            Text(
              address,
              style: const TextStyle(
                fontSize: 15,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
        Padding(
          padding: const EdgeInsets.only(left: 30),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              const SizedBox(height: 4),
              Row(
                children: [
                  Text(
                    detail,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey[600],
                    ),
                  ),
                  Container(
                    margin: const EdgeInsets.symmetric(horizontal: 8),
                    width: 1,
                    height: 12,
                    color: const Color(0xFFEEEEEE),
                  ),
                  Text(
                    floor,
                    style: TextStyle(
                      fontSize: 14,
                      color: Colors.grey[600],
                    ),
                  ),
                  const SizedBox(width: 8),
                  Container(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 6,
                      vertical: 2,
                    ),
                    decoration: BoxDecoration(
                      color: hasElevator 
                          ? Colors.green.withOpacity(0.1)
                          : Colors.orange.withOpacity(0.1),
                      borderRadius: BorderRadius.circular(4),
                    ),
                    child: Row(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        Icon(
                          hasElevator ? Icons.elevator : Icons.elevator_outlined,
                          size: 12,
                          color: hasElevator ? Colors.green : Colors.orange,
                        ),
                        const SizedBox(width: 4),
                        Text(
                          hasElevator ? '有电梯' : '无电梯',
                          style: TextStyle(
                            fontSize: 12,
                            color: hasElevator ? Colors.green : Colors.orange,
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
        if (showDivider)
          const Padding(
            padding: EdgeInsets.symmetric(vertical: 12),
            child: Divider(
              height: 0.5,
              color: Color(0xFFEEEEEE),
            ),
          ),
      ],
    );
  }

  Widget _buildServiceCard() {
    return Container(
      padding: const EdgeInsets.all(15),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Container(
                padding: const EdgeInsets.all(8),
                decoration: BoxDecoration(
                  color: Colors.green.withOpacity(0.1),
                  shape: BoxShape.circle,
                ),
                child: const Icon(
                  Icons.local_shipping,
                  color: Colors.green,
                  size: 20,
                ),
              ),
              const SizedBox(width: 10),
              const Text(
                '服务信息',
                style: TextStyle(
                  fontSize: 16,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ],
          ),
          const SizedBox(height: 15),
          // 客户信息
          _buildServiceItem(
            '联系人',
            '张先生 13800138000',
            showDivider: true,
          ),
          // 服务类型
          _buildServiceItem(
            '服务类型',
            '大件搬运',
            showDivider: true,
          ),
          // 预约时间
          _buildServiceItem(
            '预约时间',
            '2024-02-05 14:30',
            showDivider: true,
          ),
          // 物品信息
          _buildServiceItem(
            '物品信息',
            '沙发 1件、床 1件、衣柜 2件',
            showDivider: true,
          ),
          // 搬运距离
          _buildServiceItem(
            '搬运距离',
            '约 5.8 公里',
            showDivider: true,
          ),
          // 电梯情况
          _buildServiceItem(
            '电梯情况',
            '搬出有电梯，搬入无电梯（6楼）',
            showDivider: true,
          ),
          // 备注信息
          _buildServiceItem(
            '备注信息',
            '请师傅带工具，小心搬运',
          ),
        ],
      ),
    );
  }

  Widget _buildServiceItem(String label, String value, {bool showDivider = false}) {
    return Column(
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              label,
              style: TextStyle(
                fontSize: 14,
                color: Colors.grey[600],
              ),
            ),
            Text(
              value,
              style: const TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w500,
              ),
            ),
          ],
        ),
        if (showDivider)
          const Padding(
            padding: EdgeInsets.symmetric(vertical: 12),
            child: Divider(
              height: 0.5,
              color: Color(0xFFEEEEEE),
            ),
          ),
      ],
    );
  }

  Widget _buildOrderInfoCard() {
    return Builder(
      builder: (context) => Container(
        padding: const EdgeInsets.all(15),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.black.withOpacity(0.05),
              blurRadius: 10,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(8),
                  decoration: BoxDecoration(
                    color: Colors.purple.withOpacity(0.1),
                    shape: BoxShape.circle,
                  ),
                  child: const Icon(
                    Icons.receipt,
                    color: Colors.purple,
                    size: 20,
                  ),
                ),
                const SizedBox(width: 10),
                const Text(
                  '订单信息',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ],
            ),
            const SizedBox(height: 15),
            _buildOrderInfoItem('订单编号', 'SO2024020512345'),
            _buildOrderInfoItem('下单时间', order.time),
            _buildOrderInfoItem('支付方式', '在线支付'),
            _buildDivider(),
            // 金额明细
            _buildOrderInfoItem('基础服务费', '¥299'),
            _buildOrderInfoItem('楼层费', '¥50'),
            _buildOrderInfoItem('远程服务费', '¥30'),
            _buildOrderInfoItem('优惠券', '-¥20'),
            _buildDivider(),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                const Text(
                  '订单金额',
                  style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                Text(
                  '¥${order.amount}',
                  style: TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.w600,
                    color: Theme.of(context).colorScheme.primary,
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildOrderInfoItem(String label, String value) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: TextStyle(
              fontSize: 14,
              color: Colors.grey[600],
            ),
          ),
          Text(
            value,
            style: const TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w500,
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildDivider() {
    return const Padding(
      padding: EdgeInsets.symmetric(vertical: 12),
      child: Divider(
        height: 0.5,
        color: Color(0xFFEEEEEE),
      ),
    );
  }

  Widget _buildBottomButtons(BuildContext context) {
    return Container(
      padding: const EdgeInsets.fromLTRB(15, 10, 15, 0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: const BorderRadius.vertical(
          top: Radius.circular(16),
        ),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withOpacity(0.05),
            blurRadius: 10,
            offset: const Offset(0, -2),
          ),
        ],
      ),
      child: SafeArea(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            // 订单金额部分
            Row(
              children: [
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                        '订单金额',
                        style: TextStyle(
                          fontSize: 14,
                          color: Color(0xFF666666),
                        ),
                      ),
                      const SizedBox(height: 2),
                      Text(
                        '¥${order.amount}',
                        style: TextStyle(
                          fontSize: 20,
                          fontWeight: FontWeight.w600,
                          color: Theme.of(context).colorScheme.primary,
                        ),
                      ),
                    ],
                  ),
                ),
                // 操作按钮
                Wrap(
                  spacing: 10,
                  children: order.actions.map((action) {
                    bool isPrimary = action == '立即支付' || action == '确认完成';
                    return SizedBox(
                      height: 36,
                      child: FilledButton(
                        onPressed: () => _handleActionButtonTap(context, action),
                        style: FilledButton.styleFrom(
                          backgroundColor: isPrimary 
                              ? Theme.of(context).colorScheme.primary
                              : Colors.white,
                          foregroundColor: isPrimary 
                              ? Colors.white 
                              : Theme.of(context).colorScheme.primary,
                          elevation: 0,
                          side: BorderSide(
                            color: isPrimary 
                                ? Theme.of(context).colorScheme.primary 
                                : Colors.grey[300]!,
                          ),
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(8),
                          ),
                          padding: const EdgeInsets.symmetric(
                            horizontal: 20,
                          ),
                          minimumSize: const Size(88, 36),
                        ),
                        child: Text(
                          action,
                          style: TextStyle(
                            fontSize: 14,
                            fontWeight: isPrimary ? FontWeight.w600 : FontWeight.w500,
                          ),
                        ),
                      ),
                    );
                  }).toList(),
                ),
              ],
            ),
            const SizedBox(height: 10),
          ],
        ),
      ),
    );
  }

  void _handleActionButtonTap(BuildContext context, String action) {
    switch (action) {
      case '立即支付':
        _showPaymentSheet(context);
        break;
      case '取消订单':
        _showCancelConfirmDialog(context);
        break;
      case '确认完成':
        _showCompleteConfirmDialog(context);
        break;
      case '删除订单':
        _showDeleteConfirmDialog(context);
        break;
      case '再次下单':
      case '重新下单':
        _handleReorder(context);
        break;
    }
  }

  void _showPaymentSheet(BuildContext context) {
    String selectedPayment = '支付宝支付'; // 默认选中支付宝

    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      isScrollControlled: true,
      builder: (context) => StatefulBuilder( // 使用 StatefulBuilder 来管理状态
        builder: (context, setState) => Container(
          constraints: BoxConstraints(
            maxHeight: MediaQuery.of(context).size.height * 0.85,
          ),
          decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.vertical(
              top: Radius.circular(16),
            ),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 标题栏
              Container(
                height: 52,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color(0xFFEEEEEE),
                      width: 0.5,
                    ),
                  ),
                ),
                child: const Text(
                  '订单支付',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
              // 内容区域（可滚动）
              Flexible(
                child: SingleChildScrollView(
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      // 支付金额
                      Container(
                        padding: const EdgeInsets.fromLTRB(20, 20, 20, 30),
                        child: Column(
                          children: [
                            const Text(
                              '支付金额',
                              style: TextStyle(
                                fontSize: 14,
                                color: Color(0xFF666666),
                              ),
                            ),
                            const SizedBox(height: 8),
                            Text(
                              '¥${order.amount}',
                              style: TextStyle(
                                fontSize: 32,
                                fontWeight: FontWeight.w600,
                                color: Theme.of(context).colorScheme.primary,
                              ),
                            ),
                          ],
                        ),
                      ),
                      // 支付方式列表
                      _buildPaymentOption(
                        context,
                        icon: Icons.account_balance_wallet,
                        title: '支付宝支付',
                        selected: selectedPayment == '支付宝支付',
                        onTap: () {
                          setState(() {
                            selectedPayment = '支付宝支付';
                          });
                        },
                      ),
                      _buildPaymentOption(
                        context,
                        icon: Icons.payment,
                        title: '微信支付',
                        selected: selectedPayment == '微信支付',
                        onTap: () {
                          setState(() {
                            selectedPayment = '微信支付';
                          });
                        },
                      ),
                      const SizedBox(height: 8),
                    ],
                  ),
                ),
              ),
              // 底部按钮
              SafeArea(
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(15, 0, 15, 15),
                  child: FilledButton(
                    onPressed: () {
                      Navigator.of(context).pop();
                      if (selectedPayment == '支付宝支付') {
                        _handleAlipayPayment(context);
                      } else {
                        _handleWechatPayment(context);
                      }
                      // CustomToast.loading(context);
                      // Future.delayed(const Duration(seconds: 3), () {
                      //   CustomToast.hide();
                      // });
                    },
                    style: FilledButton.styleFrom(
                      backgroundColor: Theme.of(context).colorScheme.primary,
                      minimumSize: const Size.fromHeight(44),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(8),
                      ),
                    ),
                    child: const Text(
                      '立即支付',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w600,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildPaymentOption(
    BuildContext context, {
    required IconData icon,
    required String title,
    required VoidCallback onTap,
    bool selected = false,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 72,
        padding: const EdgeInsets.symmetric(horizontal: 15),
        decoration: const BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Color(0xFFEEEEEE),
              width: 0.5,
            ),
          ),
        ),
        child: Row(
          children: [
            Container(
              width: 40,
              height: 40,
              decoration: BoxDecoration(
                color: Colors.grey[100],
                borderRadius: BorderRadius.circular(8),
              ),
              child: Icon(
                icon,
                size: 24,
                color: Colors.grey[700],
              ),
            ),
            const SizedBox(width: 12),
            Expanded(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: const TextStyle(
                      fontSize: 15,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                  const SizedBox(height: 2),
                  Text(
                    title == '支付宝支付' ? '推荐使用支付宝支付' : '使用微信扫码支付',
                    style: TextStyle(
                      fontSize: 12,
                      color: Colors.grey[600],
                    ),
                  ),
                ],
              ),
            ),
            if (selected)
              Icon(
                Icons.check_circle,
                size: 20,
                color: Theme.of(context).colorScheme.primary,
              ),
          ],
        ),
      ),
    );
  }

  void _handleAlipayPayment(BuildContext context) {
    // 显示加载
    Loading.show(context);

    // 模拟支付过程
    Future.delayed(const Duration(seconds: 2), () {
      // 隐藏加载
      Loading.hide();
      
      // 显示支付成功对话框
      if (context.mounted) {  // 检查 context 是否还有效
        showDialog(
          context: context,
          barrierDismissible: false,
          builder: (dialogContext) => WillPopScope(
            onWillPop: () async => false,  // 防止返回键关闭
            child: AlertDialog(
              title: const Text(
                '支付成功',
                style: TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                ),
              ),
              content: const Text(
                '您的订单已支付成功',
                style: TextStyle(
                  fontSize: 15,
                  color: Color(0xFF666666),
                ),
              ),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(12),
              ),
              actions: [
                TextButton(
                  onPressed: () {
                    // 先关闭成功对话框，再返回订单列表
                    Navigator.of(dialogContext).pop();
                    Navigator.of(context).pop();
                  },
                  child: const Text('确定'),
                ),
              ],
            ),
          ),
        );
      }
    });
  }

  void _handleWechatPayment(BuildContext context) {
    // 实现类似的微信支付逻辑
    _handleAlipayPayment(context); // 这里暂时复用支付宝的逻辑
  }

  void _showCancelConfirmDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text(
          '取消订单',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        content: const Text(
          '确定要取消该订单吗？',
          style: TextStyle(
            fontSize: 15,
            color: Color(0xFF666666),
          ),
        ),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('再想想'),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              // 处理取消订单
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('订单已取消')),
              );
              Navigator.pop(context); // 返回订单列表
            },
            child: Text(
              '确定取消',
              style: TextStyle(
                color: Theme.of(context).colorScheme.error,
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _showCompleteConfirmDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text(
          '确认完成',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        content: const Text(
          '确认服务已完成吗？',
          style: TextStyle(
            fontSize: 15,
            color: Color(0xFF666666),
          ),
        ),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              // 处理确认完成
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('订单已完成')),
              );
              Navigator.pop(context); // 返回订单列表
            },
            child: Text(
              '确定',
              style: TextStyle(
                color: Theme.of(context).colorScheme.primary,
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _showDeleteConfirmDialog(BuildContext context) {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text(
          '删除订单',
          style: TextStyle(
            fontSize: 18,
            fontWeight: FontWeight.w600,
          ),
        ),
        content: const Text(
          '确定要删除该订单吗？',
          style: TextStyle(
            fontSize: 15,
            color: Color(0xFF666666),
          ),
        ),
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(12),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('取消'),
          ),
          TextButton(
            onPressed: () {
              Navigator.pop(context);
              // 处理删除订单
              ScaffoldMessenger.of(context).showSnackBar(
                const SnackBar(content: Text('订单已删除')),
              );
              Navigator.pop(context); // 返回订单列表
            },
            child: Text(
              '确定',
              style: TextStyle(
                color: Theme.of(context).colorScheme.error,
              ),
            ),
          ),
        ],
      ),
    );
  }

  void _handleReorder(BuildContext context) {
    // 跳转到首页重新下单
    Navigator.of(context).pushAndRemoveUntil(
      MaterialPageRoute(
        builder: (context) => const MainPage(initialIndex: 0),
      ),
      (route) => false,
    );
  }

  String _getStatusDescription() {
    switch (order.status) {
      case 'serving':
        return '师傅正在为您服务';
      case 'pending':
        return '请在24小时内完成支付';
      case 'completed':
        return '感谢您的使用';
      case 'cancelled':
        return '订单已取消';
      default:
        return '';
    }
  }

  // 添加修改订单方法
  void _handleEditOrder(BuildContext context) {
    showModalBottomSheet(
      context: context,
      backgroundColor: Colors.transparent,
      builder: (context) => Container(
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.vertical(
            top: Radius.circular(16),
          ),
        ),
        child: SafeArea(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                height: 52,
                alignment: Alignment.center,
                decoration: const BoxDecoration(
                  border: Border(
                    bottom: BorderSide(
                      color: Color(0xFFEEEEEE),
                      width: 0.5,
                    ),
                  ),
                ),
                child: const Text(
                  '修改订单',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
              // 修改选项
              _buildEditOption(
                context,
                icon: Icons.access_time,
                title: '修改预约时间',
                onTap: () {
                  Navigator.pop(context);
                  // 跳转到修改时间页面
                },
              ),
              _buildEditOption(
                context,
                icon: Icons.location_on,
                title: '修改地址信息',
                onTap: () {
                  Navigator.pop(context);
                  // 跳转到修改地址页面
                },
              ),
              _buildEditOption(
                context,
                icon: Icons.inventory_2,
                title: '修改物品信息',
                onTap: () {
                  Navigator.pop(context);
                  // 跳转到修改物品页面
                },
              ),
              const SizedBox(height: 8),
              // 取消按钮
              Padding(
                padding: const EdgeInsets.fromLTRB(15, 0, 15, 15),
                child: SizedBox(
                  width: double.infinity,
                  height: 44,
                  child: FilledButton(
                    onPressed: () => Navigator.pop(context),
                    style: FilledButton.styleFrom(
                      backgroundColor: Colors.white,
                      foregroundColor: Colors.grey[600],
                      elevation: 0,
                      side: BorderSide(
                        color: Colors.grey[300]!,
                      ),
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(22),
                      ),
                    ),
                    child: const Text(
                      '取消',
                      style: TextStyle(
                        fontSize: 15,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildEditOption(
    BuildContext context, {
    required IconData icon,
    required String title,
    required VoidCallback onTap,
  }) {
    return InkWell(
      onTap: onTap,
      child: Container(
        height: 52,
        padding: const EdgeInsets.symmetric(horizontal: 15),
        decoration: const BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Color(0xFFEEEEEE),
              width: 0.5,
            ),
          ),
        ),
        child: Row(
          children: [
            Icon(
              icon,
              size: 20,
              color: Colors.grey[600],
            ),
            const SizedBox(width: 10),
            Text(
              title,
              style: const TextStyle(
                fontSize: 15,
                fontWeight: FontWeight.w500,
              ),
            ),
            const Spacer(),
            Icon(
              Icons.arrow_forward_ios,
              size: 16,
              color: Colors.grey[400],
            ),
          ],
        ),
      ),
    );
  }
} 